﻿using System;
using System.Windows;
using GalaSoft.MvvmLight.Messaging;
using MVVMCore.Messaging;

namespace HousholdISv2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            Messenger.Register<DialogMessage>(this, ProcessMessage);
            Messenger.Register<CustomDialogMessage>(this, this.ProcessMessage);

            InitializeComponent();
            this.Closed += MainWindowClosed;
        }

        private static Messenger Messenger
        {
            get { return Messenger.Default; }
        }

        private static void ProcessMessage(DialogMessage message)
        {
            var result = MessageBox.Show(
                message.Content,
                message.Caption,
                message.Button,
                message.Icon);
            message.Callback(result);
        }

        private void MainWindowClosed(object sender, EventArgs e)
        {
            Messenger.Unregister<DialogMessage>(this, ProcessMessage);
            Messenger.Unregister<CustomDialogMessage>(this, this.ProcessMessage);
        }

        private void ProcessMessage(CustomDialogMessage message)
        {
            var dialog =
                new CustomDialog
                    {
                        Title = message.Title,
                        Content = message.Content,
                        Owner = this
                    };
            dialog.ShowDialog();
        }
    }
}